# -*- coding: utf-8 -*-
# coding=utf-8
'''
Created on 2014-3-20
计算商品的所有价格，获取最低价和最高价，排除掉0
@author: SimonFu
'''
import os
import sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)

import codecs
import csv
import DBCenter


            
def Run():
    select_all_item_sql = '''
        select * from item
    '''
    all_item_list = DBCenter.rc_db.query(select_all_item_sql)
    
    for item_en in all_item_list:
        all_item_price_sql = '''
            select * from item_price
            where item_id = '%s'
        ''' % item_en.get('item_id')
        all_item_price_list = DBCenter.rc_db.query(all_item_price_sql)
        
        min_price = 0
        max_price = 0
        
        for item_price_en in all_item_price_list:
            #最低价
            if(min_price == 0):
                min_price = item_price_en.get('sale_price')
            else:
                if(item_price_en.get('sale_price') < min_price):
                    min_price = item_price_en.get('sale_price')
            
            #最高价
            if(max_price == 0):   
                max_price = item_price_en.get('sale_price')
            else:
                if(item_price_en.get('sale_price') > max_price):
                    max_price = item_price_en.get('sale_price')

        update_style_price_sql = '''
            update style
                set `min_price` = %s,
                    `max_price` = %s
            where style_id = '%s'
        ''' % (min_price, max_price, item_en.get('style_id'))
        DBCenter.rc_db.execute(update_style_price_sql)
        
Run()
# print os.getcwd()
